<?php

namespace app\index\controller;

use app\BaseController;
use app\IndexBaseController;
use think\facade\Cache;
use think\facade\Db;
use think\facade\fetch;
use think\App;
use think\facade\Log;
use think\facade\View;

class Login extends BaseController
{
    //继承
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    public function index()
    {
        return View::fetch('index');
    }

    public function login()
    {
        $username = $this->_param['username'] ?? '';
        $psw = $this->_param['password'] ?? '';
        if (empty($username) || empty($psw))
            return $this->error('Please enter your user name or password');
        $filter = [
            "userName|email" => $username
        ];
        $userInfo = Db::name("user")->where($filter)->find();
        if ($username != $userInfo['userName'] || $psw != $userInfo['passwd'])
            return $this->error('The user name or password is incorrect');
        $token = $this->getToken($userInfo);
        $user = [
            "userName" => $userInfo['userName'],
            "is_wholesale" => $userInfo['is_wholesale'],
            "email" => $userInfo['email']
        ];
        return $this->success(['token'=>$token,'user'=>$user]);
    }

    public function getToken($userInfo)
    {
        $userInfo['time'] = time();
        $token = sha1($this->_ip . $userInfo['userName'] . $userInfo['is_wholesale'] . $userInfo['id']);
        Log::info($token);
        setCache($token, $userInfo, 60 * 60 * 24);
        return $token;
    }

    //注册用户
    public function registerUser() {
        $param = $this->_param;
        $time = time();
        $user = Db::name('user')->where(['userName'=>$param['name']])->find();
        if($user) {
            return $this->error("The user name already exists");
        }
        //开启事务
        Db::startTrans();
        try {
            //没有用户信息，先注册
            $user = [
                'userName' => $param['name'],
                'passwd' => $param['pass'],
//                'userTel' => $param['tel'],
//                'email' => $param['email'],
                'create_time' => $time,
                'update_time' => $time
            ];
            $user_id = Db::name('user')->insertGetId($user);
            $userInfo = Db::name("user")->where(['id'=>$user_id])->find();
            $token = $this->getToken($userInfo);
            $user = [
                "userName" => $userInfo['userName'],
                "is_wholesale" => $userInfo['is_wholesale'],
                "email" => $userInfo['email']
            ];
            // 提交事务
            Db::commit();
            return $this->success(['token'=>$token,'user'=>$user]);
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->error("System error, please try again");
        }
    }

    //注册批发商
    public function registerWholesale() {
        $param = $this->_param;
        $time = time();
        $user = Db::name('user')->where(['userName'=>$param['name']])->find();
        if($user) {
            return $this->error("The user name already exists");
        }
        //开启事务
        Db::startTrans();
        try {
            if($param['user_id'] == 0) {
                //没有用户信息，先注册
                $user = [
                    'userName' => $param['name'],
                    'passwd' => $param['pass'],
                    'userTel' => $param['tel'],
                    'email' => $param['email'],
                    'create_time' => $time,
                    'update_time' => $time
                ];
                $user_id = Db::name('user')->insertGetId($user);
            }else {
                //存在用户信息
                $user_id = $param['user_id'];
            }
            $wholesale = [
                'user_id' => $user_id,
                'bus_name' => $param['bus_name'],
                'license' => $param['license'],
                'create_time' => $time,
                'status' => 1
            ];
            $res = Db::name('wholesale_user_log')->insert($wholesale);

            $userInfo = Db::name("user")->where(['id'=>$user_id])->find();
            $token = $this->getToken($userInfo);
            $user = [
                "userName" => $userInfo['userName'],
                "is_wholesale" => $userInfo['is_wholesale'],
                "email" => $userInfo['email']
            ];
            // 提交事务
            Db::commit();
            return $this->success(['token'=>$token,'user'=>$user]);
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->error("System error, please try again");
        }
    }
}
